package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsSetBox;
import com.jy.wms.service.WmsSetBoxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName: WmsSetBoxController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author liyi
 * @Date Fri Jan 26 10:47:41 CST 2018
 */
@RestController
@RequestMapping("/wmsSetBox")
@Api(value = "/wmsSetBox", description = "集拼箱")
public class  WmsSetBoxController extends BaseController<WmsSetBoxService,WmsSetBox> {

    @PostMapping(value = "/getXmx")
    @ApiOperation(value = "集拼箱",response = JSONObject.class, notes = "查询箱编号 map包含 xbh（必填）,dz（选填)")
    public @ResponseBody JSONObject getXmx(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            json = baseService.getXmx(map,session);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value = "/getXx")
    @ApiOperation(value = "集拼箱",response = JSONObject.class, notes = "查询箱型")
    public @ResponseBody JSONArray getXx(HttpSession session) {
        JSONArray json = new JSONArray();
        try {
            json = JSONArray.fromObject( baseService.getXx(session));
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "集拼箱",response = JSONObject.class, notes = "新增集拼箱")
    public @ResponseBody JSONObject add(@RequestBody WmsSetBox wmsSetBox, HttpSession session) {
        JSONArray json = new JSONArray();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            wmsSetBox.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
             baseService.baseInsertSelective(wmsSetBox);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增集拼箱成功","", JSONObject.fromObject(wmsSetBox).toString());
        }catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增集拼箱成功","", JSONObject.fromObject(wmsSetBox).toString());
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增集拼箱失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增集拼箱失败",e.getMessage(),"");
        }
    }

    @GetMapping(value = "/print/{id}")
    @ApiOperation(value = "集拼箱", response = JSONObject.class, notes = "打印显示数据")
    public @ResponseBody  JSONObject print(HttpSession session,@PathVariable String id){
        JSONObject json = getJSONObject();
        try {
            json = baseService.print(id,session);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @GetMapping(value = "/setXx/{id}/{xxid}")
    @ApiOperation(value = "集拼箱", response = JSONObject.class, notes = "修改箱型")
    public @ResponseBody  JSONObject setXx(HttpSession session, @PathVariable String xxid, @PathVariable String id){
        try {
            baseService.setXx(xxid,id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改箱型成功","", "");
        }catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改箱型成功","", "");
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改箱型失败",e.getMessage(),"");
        }
    }

}
